﻿using System;
using System.Drawing;
using System.Windows.Forms;


namespace Marmat.Forms
{
    public partial class TopicBarCollectionForm : System.Windows.Forms.Form
    {

        /* TODO ERROR: Skipped RegionDirectiveTrivia */
        public TopicBarCollectionForm(TopicBar TopicBar)
        {

            // 此调用是设计器所必需的。
            this.InitializeComponent();

            // 在 InitializeComponent() 调用之后添加任何初始化。
            this._TopicBar = TopicBar;
            TreeViewOptimization.Bind(this.TreeView1);
            this._Pages = new TopicBarPageCollection(null);
            this._Pages.AddRange(TopicBar.Pages.ToArray());
            Button6.Name = "Button6";
            Button5.Name = "Button5";
            Button4.Name = "Button4";
            Button3.Name = "Button3";
            TreeView1.Name = "TreeView1";
            PropertyGrid1.Name = "PropertyGrid1";
            ContextMenuStrip1.Name = "ContextMenuStrip1";
            Button7.Name = "Button7";
        }
        /* TODO ERROR: Skipped EndRegionDirectiveTrivia */
        /* TODO ERROR: Skipped RegionDirectiveTrivia */
        private TopicBar _TopicBar;
        private TopicBarPageCollection _Pages;
        /* TODO ERROR: Skipped EndRegionDirectiveTrivia */
        /* TODO ERROR: Skipped RegionDirectiveTrivia */    /// <summary>
                                                           /// 生成目录树
                                                           /// </summary>
        private void Method_0()
        {
            foreach (TopicBarPage Page in this._Pages)
            {
                var nd = this.TreeView1.Nodes.Add(Page.Name);
                nd.Tag = Page;
                foreach (TopicBarItem item in Page.Items)
                {
                    var znd = nd.Nodes.Add(item.Name);
                    znd.Tag = item;
                }
            }
        }

        /// <summary>
        /// 返回最新项集合
        /// </summary>
        internal TopicBarPageCollection Method_1()
        {
            var pages = new TopicBarPageCollection(this._TopicBar);
            foreach (System.Windows.Forms.TreeNode nd in this.TreeView1.Nodes)
            {
                pages.Add((TopicBarPage)nd.Tag);
                ((TopicBarPage)nd.Tag).Items.Clear();
                foreach (System.Windows.Forms.TreeNode znd in nd.Nodes)
                    ((TopicBarPage)nd.Tag).Items.Add(znd.Tag?.ToString());
            }

            return pages;
        }
        /* TODO ERROR: Skipped EndRegionDirectiveTrivia */
        /* TODO ERROR: Skipped RegionDirectiveTrivia */
        private void NavigateCollectionForm_Load(object sender, EventArgs e)
        {
            this.Method_0();
            this.TreeView1.ExpandAll();
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            var p = this.Button3.PointToScreen(new Point(0, 0));
            this.ContextMenuStrip1.Show(p.X, p.Y + this.Button3.Height);
        }

        private void TreeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            this.PropertyGrid1.SelectedObject = e.Node.Tag;
        }

        private void ContextMenuStrip1_ItemClicked(object sender, System.Windows.Forms.ToolStripItemClickedEventArgs e)
        {
            switch (e.ClickedItem.Text ?? "")
            {
                case "添加TopicBarPage":
                    {
                        var nd = this.TreeView1.Nodes.Add(GlobalMethod.NoDuplicatevalues(this.TreeView1, "面板", 1));
                        var Page = new TopicBarPage();
                        Page.Name = nd.Text;
                        nd.Tag = Page;
                        this.TreeView1.SelectedNode = nd;
                        break;
                    }

                case "添加TopicBarItem":
                    {
                        if (this.TreeView1.Nodes.Count == 0)
                        {
                            System.Windows.Forms.MessageBox.Show("请先添加TopicBarPage", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        else if (this.TreeView1.SelectedNode is null)
                        {
                            System.Windows.Forms.MessageBox.Show("请先选择要进行添加的TopicBarPage", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                        else
                        {
                            var nd = new System.Windows.Forms.TreeNode(GlobalMethod.NoDuplicatevalues(this.TreeView1, "列表", 1));
                            var item = new TopicBarItem();
                            item.Name = nd.Text;
                            nd.Tag = item;
                            if (this.TreeView1.SelectedNode.Level == 0)
                            {
                                this.TreeView1.SelectedNode.Nodes.Add(nd);
                            }
                            else
                            {
                                this.TreeView1.SelectedNode.Parent.Nodes.Add(nd);
                            }

                            this.TreeView1.SelectedNode = nd;
                        }

                        break;
                    }
            }

            this.TreeView1.ExpandAll();
        }

        private void PropertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            switch (e.ChangedItem.Label ?? "")
            {
                case "Name":
                    {
                        this.TreeView1.SelectedNode.Text = Convert.ToString(e.ChangedItem.Value);
                        break;
                    }
            }
        }

        private void Button5_Click(object sender, EventArgs e)
        {
            TreeViewOptimization.MoveUp(this.TreeView1.SelectedNode);
        }

        private void Button6_Click(object sender, EventArgs e)
        {
            TreeViewOptimization.MoveDown(this.TreeView1.SelectedNode);
        }

        private void Button7_Click(object sender, EventArgs e)
        {
            DialogResult Result;
            Result = System.Windows.Forms.MessageBox.Show("您确定要清空所有项吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (Result == DialogResult.Yes)
            {
                this.TreeView1.Nodes.Clear();
            }
        }

        private void Button4_Click(object sender, EventArgs e)
        {
            if (this.TreeView1.SelectedNode is null)
            {
                System.Windows.Forms.MessageBox.Show("您未选中要删除的项,请先选中后再进行删除!");
            }
            else
            {
                DialogResult Result;
                Result = System.Windows.Forms.MessageBox.Show("您确定要删除当前选择项吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (Result == DialogResult.Yes)
                {
                    this.TreeView1.SelectedNode.Remove();
                }
            }
        }
        /* TODO ERROR: Skipped EndRegionDirectiveTrivia */
    }
}